import java.util.List;  // 新增：导入List类
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ScoreManagementSystem sys = new ScoreManagementSystem();
        Scanner sc = new Scanner(System.in);
        int choice;

        do {
            System.out.println("\n===== 学生成绩管理系统 =====");
            System.out.println("1. 添加学生    2. 添加课程    3. 添加成绩");
            System.out.println("4. 查课程成绩  5. 算GPA      6. 显示学生");
            System.out.println("7. 显示课程    8. 保存到桌面  0. 退出");
            System.out.print("请选择操作：");
            choice = sc.nextInt();
            sc.nextLine(); // 清空缓冲区

            switch (choice) {
                case 1:
                    System.out.print("输入学号：");
                    String sid = sc.nextLine();
                    System.out.print("输入姓名：");
                    String name = sc.nextLine();
                    System.out.print("输入班级：");
                    String cls = sc.nextLine();
                    sys.addStudent(new Student(sid, name, cls));
                    System.out.println("✅ 学生添加成功");
                    break;

                case 2:
                    System.out.print("输入课程编号：");
                    String cid = sc.nextLine();
                    System.out.print("输入课程名称：");
                    String cname = sc.nextLine();
                    System.out.print("输入学分：");
                    double credit = sc.nextDouble();
                    sys.addCourse(new Course(cid, cname, credit));
                    System.out.println("✅ 课程添加成功");
                    break;

                case 3:
                    System.out.print("输入学生学号：");
                    Student s = sys.getStudents().stream()
                            .filter(st -> st.getStudentId().equals(sc.nextLine()))
                            .findFirst().orElse(null);
                    System.out.print("输入课程编号：");
                    Course c = sys.getCourses().stream()
                            .filter(co -> co.getCourseId().equals(sc.nextLine()))
                            .findFirst().orElse(null);

                    if (s != null && c != null) {
                        System.out.print("输入分数：");
                        try {
                            Score newScore = new Score(s, c, sc.nextDouble());
                            boolean isAdded = sys.addScore(newScore);
                            if (isAdded) System.out.println("✅ 成绩添加成功");
                            else System.out.println("❌ 该学生已存在这门课程的成绩，无法重复添加！");
                        } catch (InvalidScoreException e) {
                            System.out.println("❌ " + e.getMessage());
                        }
                    } else {
                        System.out.println("❌ 学生或课程不存在");
                    }
                    break;

                case 4:
                    System.out.print("输入课程编号：");
                    List<Score> courseScores = sys.getScoresByCourse(sc.nextLine());
                    if (courseScores.isEmpty()) System.out.println("ℹ️  未找到该课程的成绩");
                    else courseScores.forEach(System.out::println);
                    break;

                case 5:
                    System.out.print("输入学生学号：");
                    System.out.printf("✅ GPA：%.2f\n", sys.calcGPA(sc.nextLine()));
                    break;

                case 6:
                    System.out.println("===== 所有学生 =====");
                    sys.getStudents().forEach(System.out::println);
                    break;

                case 7:
                    System.out.println("===== 所有课程 =====");
                    sys.getCourses().forEach(System.out::println);
                    break;

                case 8:
                    sys.saveData();
                    break;

                case 0:
                    System.out.println("👋 程序结束，再见！");
                    break;

                default:
                    System.out.println("❌ 无效选择，请重新输入");
            }
        } while (choice != 0);

        sc.close();
    }
}